clear
clear all
cap log close

log using "log-files/2_Figure_H3.smcl", replace smcl

timer on 2

*------------*
* Figure H.3 *
*------------*
	* Figure H.3.a
	*-------------
		use "data/outputs/computo_verificadortrepdate_missingsatthend.dta", clear	

		* Nulos share
		gen nulosshare = Nulos / total_nbnn

		* Margin
		gen margin_nbnn = mshare_nbnn - cshare_nbnn

		* Recinto ID
		egen reci_id = group(Pais Dep Prov Muni Loc Reci)
			   /* Note, in constructing the data set we made a correction
			   such that recinto names are unique within localities. */

		foreach var of varlist mshare_nbnn cshare_nbnn margin_nbnn nulosshare {
			egen reci_mean = mean(`var'), by(reci_id)
			gen `var'_dm = `var' - reci_mean 
			drop reci_mean
			qui su `var'
			gen `var'_dm_plus = `var'_dm + `r(mean)' // Add overall mean for scale
		}
			
		* Cutoff
		quietly sum pcs_nbnn if trep_date_verificador >= tc(20oct2019 19:40:57) & ///
			trep_date_verificador <= tc(20oct2019 19:40:58), detail
		local cutoff = `r(min)' 
		local label = round(`cutoff', .01)

		lprobust margin_nbnn_dm pcs_nbnn, genvars

		twoway (line lprobust_gx_bc lprobust_eval, ///
					lcolor(gs6) lwidth(medthick)) ///
			(line lprobust_CI_l_rb lprobust_eval, ///
				lcolor(gs6) lwidth(thin) lpattern(dash)) ///
			(line lprobust_CI_r_rb lprobust_eval, ///
				lcolor(gs6) lwidth(thin) lpattern(dash)), ///
			graphregion(color(white)) ///
			xline(`cutoff', lcolor(black) lpattern(dash)) ///
			ylab(, angle(0)) ///
			xlab(0 `cutoff' "`label'" 1) ///
			ytitle("Booth-level MAS Margin Minus Precinct Mean") ///
			xtitle("Proportion of Preliminary Vote Counted") ///
			xsize(7) ysize(5) ///
			legend(off)
		graph export "outputs/generated/Figure_H3a.pdf", replace

	* Figure H.3.b
	*-------------
		cap drop lprobust*
		lprobust margin_nbnn_dm pcs_nbnn, genvars h(0.15)

		twoway (line lprobust_gx_bc lprobust_eval, ///
					lcolor(gs6) lwidth(medthick)) ///
			(line lprobust_CI_l_rb lprobust_eval, ///
				lcolor(gs6) lwidth(thin) lpattern(dash)) ///
			(line lprobust_CI_r_rb lprobust_eval, ///
				lcolor(gs6) lwidth(thin) lpattern(dash)), ///
			graphregion(color(white)) ///
			xline(`cutoff', lcolor(black) lpattern(dash)) ///
			yline(0.5, lcolor(black)) ///
			ylab(, angle(0)) ///
			xlab(0 `cutoff' "`label'" 1) ///
			ytitle("Booth-level MAS Margin Minus Precinct Mean") ///
			xtitle("Proportion of Preliminary Vote Counted") ///
			xsize(7) ysize(5) ///
			legend(off)
		graph export "outputs/generated/Figure_H3b.pdf", replace

*------------------------------------------------------------------------------*
timer off 2
timer list 2

log close
clear all
